<?php
namespace Admin\Controller;

use Think\Controller;
class TaskController extends ComController
{
   
    public function tasklist() {
        $adcate = $this->taskCate();
        $Ad = M('Task');
        $ads = $Ad->select();
        foreach ($ads as $key => &$value) {
             foreach ($adcate as $v) {
                 if($v['type']==$value['type']){
                       $ads[$key]['name'] = $v['name'];
                 }
         }
        }
        $this->assign('ads', $ads);        
        $this->display();
    }
    
    
    public function add() {
         $adcate = $this->taskCate();
         $this->assign('adcate', $adcate);
        $this->display('form');
    }
    
    public function edit($id) {
        $id = intval($id);
        $adcate = $this->taskCate();
        $ad = D('Task')->find($id);
        $this->assign('adcate', $adcate);
        $this->assign('ad', $ad);
        $this->display('form');
    }
    
    public function update() {
        $Ad = M('Task');
         $data = I('post.data');
         $data['add_time'] = time();
         if($data['id']){
           $Ad->data($data)->save();
           $this->success('编辑任务成功');
         }  else {
           $Ad->data($data)->add(); 
           $this->success('添加任务成功');
         }
         
    }
        
    public function taskCate() {
        $data =[
                [
                'type'=>'once',
                'name'=>'新手任务',
            ],
                  [
                'type'=>'day',
                'name'=>'每日任务',
            ],
            ];
        return $data;
    }
    
}